home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / shelly / shelly.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  20KB  |  873 lines

  1. /*****************************************************/
  2. /* ShellyV1.2: The ShellShapeGenerator by:           */
  3. /*         RANDi                                     */
  4. /*              (rschultz@informatik.uni-rostock.de) */
  5. /*****************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <memory.h>
  12.  
  13. #include "shelly.h"
  14.  
  15. int countw=0, counth=0, countp=0;
  16. double camx=0,camy=500,camz=500;
  17.  
  18.  
  19. /********************************************/
  20. /*cot:                                      */
  21. /********************************************/
  22. double cot(double in)
  23. {
  24.  if (sin(in) == 0) 
  25.   return(0);
  26.  else 
  27.   return(cos(in)/sin(in));
  28. }
  29.  
  30.  
  31. /********************************************/
  32. /*round:                                    */
  33. /********************************************/
  34. double round(double in)
  35. {
  36.  if((floor(in)-in) < 0)
  37.   return(floor(in));
  38.  else
  39.   return(floor(in)+1);
  40. }
  41.  
  42.  
  43. /********************************************/
  44. /*myfree:                                   */
  45. /********************************************/
  46. int myfree(struct punkt *anker1)
  47. {
  48.  struct punkt *hp, *hpnew;
  49.  if (anker1 == NULL)
  50.   return(0);
  51.  
  52.  hp = anker1;
  53.  hpnew = hp;
  54.  do
  55.  {
  56.   hp = hpnew;
  57.   hpnew = (*hp).next;
  58.  
  59.   cfree(hp);
  60.  }
  61.  while(hpnew);
  62.  
  63. return(0);
  64. }
  65.  
  66.  
  67. /********************************************/
  68. /*mycopystr:                                */
  69. /********************************************/
  70. char *mycopystr(char *source,char *destination,char endchar)
  71. {
  72.  int ende = 1;
  73.  char *merk;
  74.  
  75.  merk = destination;        /* merk points now on destination */
  76.  while (ende)
  77.  {
  78.   if ((*source == '\0') || (*source == endchar)) 
  79.   {
  80.   ende = 0;
  81.   }
  82.   else
  83.   {
  84.    *destination = *source; 
  85.    destination++;
  86.    source++;
  87.   } /* if */
  88.  } /* while */
  89.  *destination = '\0';
  90. return(merk);
  91. }
  92.  
  93.  
  94. /********************************************/
  95. /*ReadInfile:                               */
  96. /********************************************/
  97.  
  98. void ReadInfile(struct ShellyArguments *ShellyArgs,char *fin)
  99. {
  100.  char readline[laenge], hilf[laenge], *hp;
  101.  FILE *fp;
  102.  
  103.  (*ShellyArgs).output = POV;
  104.  
  105.  fp = fopen(fin,"r");
  106.  if (fp != NULL) 
  107.  {
  108.   fprintf(stderr,"Parsing:");
  109.   while (fgets(readline,laenge,fp))
  110.   {
  111.    fprintf(stderr,".");
  112.  
  113.    hp = strstr(readline,"alpha:");
  114.    if (hp != NULL)
  115.    {
  116.     mycopystr(hp+6,hilf,'\0');
  117.     (*ShellyArgs).alpha = atof(hilf);
  118.    } /* if */
  119.  
  120.    hp = strstr(readline,"beta:");
  121.    if (hp != NULL)
  122.    {
  123.     mycopystr(hp+5,hilf,'\0');
  124.     (*ShellyArgs).beta = atof(hilf);
  125.    } /* if */
  126.    hp = strstr(readline,"phi:");
  127.    if (hp != NULL)
  128.    {
  129.     mycopystr(hp+4,hilf,'\0');
  130.     (*ShellyArgs).phi = atof(hilf);
  131.    } /* if */
  132.  
  133.    hp = strstr(readline,"my:");
  134.    if (hp != NULL)
  135.    {
  136.     mycopystr(hp+3,hilf,'\0');
  137.     (*ShellyArgs).my = atof(hilf);
  138.    } /* if */
  139.  
  140.    hp = strstr(readline,"omega:");
  141.    if (hp != NULL)
  142.    {
  143.     mycopystr(hp+6,hilf,'\0');
  144.     (*ShellyArgs).omega = atof(hilf);
  145.    } /* if */
  146.  
  147.    hp = strstr(readline,"smin:");
  148.    if (hp != NULL)
  149.    {
  150.     mycopystr(hp+5,hilf,'\0');
  151.     (*ShellyArgs).smin = atof(hilf);
  152.    } /* if */
  153.   
  154.    hp = strstr(readline,"smax:");
  155.    if (hp != NULL)
  156.    {
  157.     mycopystr(hp+5,hilf,'\0');
  158.     (*ShellyArgs).smax = atof(hilf);
  159.    } /* if */
  160.  
  161.    hp = strstr(readline,"sd:");
  162.    if (hp != NULL)
  163.    {
  164.     mycopystr(hp+3,hilf,'\0');
  165.     (*ShellyArgs).sd = atof(hilf);
  166.    } /* if */
  167.  
  168.    hp = strstr(readline,"A:");
  169.    if (hp != NULL)
  170.    {
  171.     mycopystr(hp+2,hilf,'\0');
  172.     (*ShellyArgs).A = atof(hilf);
  173.    } /* if */
  174.  
  175.    hp = strstr(readline,"a:");
  176.    if (hp != NULL)
  177.    {
  178.     mycopystr(hp+2,hilf,'\0');
  179.     (*ShellyArgs).a = atof(hilf);
  180.    } /* if */
  181.  
  182.  
  183.    hp = strstr(readline,"b:");
  184.    if (hp != NULL)
  185.    {
  186.     mycopystr(hp+2,hilf,'\0');
  187.     (*ShellyArgs).b = atof(hilf);
  188.    } /* if */
  189.  
  190.    hp = strstr(readline,"P:");
  191.    if (hp != NULL)
  192.    {
  193.     mycopystr(hp+2,hilf,'\0');
  194.     (*ShellyArgs).P = atof(hilf);
  195.    } /* if */
  196.  
  197.    hp = strstr(readline,"W1:");
  198.    if (hp != NULL)
  199.    {
  200.     mycopystr(hp+3,hilf,'\0');
  201.     (*ShellyArgs).W1 = atof(hilf);
  202.    } /* if */
  203.  
  204.    hp = strstr(readline,"W2:");
  205.    if (hp != NULL)
  206.    {
  207.     mycopystr(hp+3,hilf,'\0');
  208.     (*ShellyArgs).W2 = atof(hilf);
  209.    } /* if */
  210.  
  211.    hp = strstr(readline,"N:");
  212.    if (hp != NULL)
  213.    {
  214.     mycopystr(hp+2,hilf,'\0');
  215.     (*ShellyArgs).N = atof(hilf);
  216.    } /* if */
  217.  
  218.    hp = strstr(readline,"L:");
  219.    if (hp != NULL)
  220.    {
  221.     mycopystr(hp+2,hilf,'\0');
  222.     (*ShellyArgs).L = atof(hilf);
  223.    } /* if */
  224.  
  225.    hp = strstr(readline,"omin:");
  226.    if (hp != NULL)
  227.    {
  228.     mycopystr(hp+5,hilf,'\0');
  229.     (*ShellyArgs).omin = atof(hilf);
  230.    } /* if */
  231.  
  232.    hp = strstr(readline,"omax:");
  233.    if (hp != NULL)
  234.    {
  235.     mycopystr(hp+5,hilf,'\0');
  236.     (*ShellyArgs).omax = atof(hilf);
  237.    } /* if */
  238.  
  239.    hp = strstr(readline,"od:");
  240.    if (hp != NULL)
  241.    {
  242.     mycopystr(hp+3,hilf,'\0');
  243.     (*ShellyArgs).od = atof(hilf);
  244.    } /* if */
  245.  
  246.    hp = strstr(readline,"POV");
  247.    if (hp != NULL)
  248.    {
  249.     (*ShellyArgs).output = POV;
  250.    } /* if */
  251.  
  252.    hp = strstr(readline,"RPL");
  253.    if (hp != NULL)
  254.    {
  255.     (*ShellyArgs).output = RPL;
  256.    } /* if */
  257.  
  258.    hp = strstr(readline,"T3D");
  259.    if (hp != NULL)
  260.    {
  261.     (*ShellyArgs).output = T3D;
  262.    } /* if */
  263.    
  264.   } /* while */
  265.  
  266.   fprintf(stderr,"\n");
  267.   if (fclose(fp) != 0) 
  268.   fprintf(stderr,"Error while closing file: %s!\n",fin);
  269.  }
  270.  else
  271.  {
  272.   fprintf(stderr,"Could not open datafile: %s!\n",fin);
  273.   exit(5);
  274.  }
  275. } /* ReadInfile */
  276.  
  277.  
  278. /********************************************/
  279. /* writepovheader: reads in camera/light's  */
  280. /*                                          */
  281. /********************************************/
  282.  
  283. void writepovheader(FILE *fp)
  284. {
  285.  
  286.  fprintf(fp,"/* POV-Scenefile generated by Shelly1.2           */\n");
  287.  fprintf(fp,"/* by RANDi: (rschultz@informatik.uni-rostock.de) */\n");
  288.  
  289.  fprintf(fp,"#declare te = pigment { color red 1 green 0 blue 0 }\n");
  290.  
  291.  fprintf(fp,"camera\n{\n location  <%.1f,%.1f,%.1f>\n",camx,camy,camz);
  292.  fprintf(fp," look_at <0, 0, 0>\n}\n");
  293.  
  294.  fprintf(fp,"object\n{\n light_source {\n");
  295.  fprintf(fp,"  <250, 500, 500> color red 1 green 1 blue 1\n }\n");
  296.  fprintf(fp,"}\n");
  297.  fprintf(fp,"object\n{\n light_source {\n");
  298.  fprintf(fp,"  <-250, 500, 500> color red 1 green 1 blue 1\n }\n");
  299.  fprintf(fp,"}\n");
  300.  
  301. } /* writepovheader */
  302.  
  303.  
  304. /********************************************/
  305. /* writepovtriangles:                       */
  306. /********************************************/
  307. int writepovtriangles(FILE *fp,struct punkt *anker1,struct punkt *anker2)
  308. {
  309.  struct punkt *p11, *p12, *p21, *p22;
  310.  
  311.  if ((anker1 == NULL) || (anker2 == NULL))
  312.   return(0);
  313.  
  314.  p11 = anker1;
  315.  p21 = anker2;
  316.  p12 = p11;
  317.  p22 = p21;
  318.  
  319.  do
  320.  {
  321.   p11 = p12;
  322.   p21 = p22;
  323.   p12 = (*p11).next;
  324.   p22 = (*p21).next;
  325.  
  326.   if ((p12 != NULL) && (p22 != NULL))
  327.   {
  328.    fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p11).x,(*p11).y,(*p11).z,(*p12).x,(*p12).y,(*p12).z,(*p21).x,(*p21).y,(*p21).z);
  329.    fprintf(fp,"pigment{te}}\n");
  330.  
  331.    fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p21).x,(*p21).y,(*p21).z,(*p12).x,(*p12).y,(*p12).z,(*p22).x,(*p22).y,(*p22).z);
  332.    fprintf(fp,"pigment{te}}\n");
  333.   }
  334.  }
  335.  while((p12 != NULL) && (p22 != NULL));
  336.  
  337.  return(0);
  338. } /* writepovtriangles */
  339.  
  340.  
  341. /********************************************/
  342. /* writerplline:                            */
  343. /********************************************/
  344. int writerplline(FILE *fp,struct punkt *anker1)
  345. {
  346.  struct punkt *p11, *p12;
  347.  
  348.  if (anker1 == NULL)
  349.   return(0);
  350.  
  351.  p11 = anker1;
  352.  p12 = p11;
  353.  
  354.  countw++;                                /* calc number of lines created */
  355.  
  356.  fprintf(fp,"%.3f  %.3f  %.3f\n",(*p11).x/100.0,(*p11).y/100.0,(*p11).z/100.0);
  357.  
  358.  do
  359.  {
  360.   p11 = p12;
  361.   p12 = (*p11).next;
  362.  
  363.   if (p12 != NULL)
  364.   {
  365.    fprintf(fp,"%.3f  %.3f  %.3f\n",(*p12).x/100.0,(*p12).y/100.0,(*p12).z/100.0);   
  366.   }
  367.  }
  368.  while(p12 != NULL);
  369.  
  370.  return(0);
  371. } /* writerplline */
  372.  
  373.  
  374. /********************************************/
  375. /* writerplfinish:                          */
  376. /*                                          */
  377. /********************************************/
  378.  
  379. void writerplfinish(FILE *fp)
  380. {
  381.   fprintf(fp,"%d\n",counth);           /* height width of the mesh */
  382.   fprintf(fp,"%d\n",countw);
  383.   
  384.   fprintf(fp,"3\n0\n255 255 255 0\n"); /* RGBA */
  385.   fprintf(fp,"\"Shell\"\n0\n");        /* Name */
  386.   fprintf(fp,"\"CEND\"\n");
  387.   fprintf(fp,"C_MESH DROP\n");
  388.  
  389. } /* writerplfinish */
  390.  
  391.  
  392. /********************************************/
  393. /* writet3dheader:                          */
  394. /*                                          */
  395. /********************************************/
  396.  
  397. void writet3dheader(FILE *fp)
  398. {
  399.  char percent = '%';
  400.  
  401.  fprintf(fp," %c T3Dlib-data-file generated by Shelly1.2\n",percent);
  402.  fprintf(fp," %c the ShellShapeGenerator by RANDi :    \n",percent);
  403.  fprintf(fp," %c (rschultz@informatik.uni-rostock.de)\n",percent);
  404.  
  405.  fprintf(fp,"OBJ Begin \"Hierarchy 1\"\n");
  406.  fprintf(fp," DESC Begin \"Object 1 at level 1 of hierarchy 1\"\n");
  407.  fprintf(fp,"  NAME \"SHELL\"\n");
  408.  fprintf(fp,"  SHAP Shape = 2\n");
  409.  fprintf(fp,"  SHAP Lamp  = 0\n");
  410.  fprintf(fp,"  POSI X=0 Y=0 Z=0\n");
  411.  fprintf(fp,"  AXIS XAxis X=1 Y=0 Z=0\n");
  412.  fprintf(fp,"  AXIS YAxis X=0 Y=1 Z=0\n");
  413.  fprintf(fp,"  AXIS ZAxis X=0 Y=0 Z=1\n");
  414.  fprintf(fp,"  SIZE X=32 Y=32 Z=32\n");
  415.  
  416. } /* writet3dheader */
  417.  
  418.  
  419. /********************************************/
  420. /* writet3dline:                            */
  421. /********************************************/
  422. int writet3dline(FILE *fp,struct punkt *anker1)
  423. {
  424.  struct punkt *p11, *p12;
  425.  
  426.  static int called = 0, count = 0;
  427.  if (anker1 == NULL)
  428.   return(0);
  429.  
  430.  if(called == 0)
  431.  {
  432.   fprintf(fp,"  PNTS PCount %d\n",countp);
  433.   called = 1;
  434.   count =-1;
  435.   countw = 0;
  436.  }
  437.  
  438.  countw++;
  439.  
  440.  p11 = anker1;
  441.  p12 = p11;
  442.   
  443.  fprintf(fp,"  PNTS Point[%d] X=%.3f Y=%.3f Z=%.3f\n",++count,(*p11).x,(*p11).y,(*p11).z);
  444.  
  445.  do
  446.  {
  447.   p11 = p12;
  448.   p12 = (*p11).next;
  449.  
  450.   if (p12 != NULL)
  451.   {
  452.    fprintf(fp,"  PNTS Point[%d] X=%.3f Y=%.3f Z=%.3f\n",++count,(*p12).x,(*p12).y,(*p12).z);
  453.   }
  454.  }
  455.  while(p12 != NULL);
  456.  
  457.  return(0);
  458. } /* writet3dline */
  459.  
  460.  
  461. /********************************************/
  462. /* writet3dfinish:                          */
  463. /*                                          */
  464. /********************************************/
  465.  
  466. void writet3dfinish(FILE *fp)
  467. {
  468.  int edges=0, faces=0, edgecount=0, facecount=0, a=0, b=0, r=0, r2=0, r3=0;
  469.  int point1=0, point2=0, edge1=0, edge2=0, edge3=0;
  470.  
  471.  fprintf(stderr,"Writing additional T3Dlib-Data ...\n");
  472.  
  473.  edges = (counth*(countw-1)+countw*(counth-1)+(counth-1)*(countw-1));
  474.                                            /* number of edges! */ 
  475.  fprintf(fp,"  EDGE ECount %d\n",edges);
  476.  
  477.  
  478.                               /* write horizontal edges */
  479.  for (a=0; a<counth;a++)
  480.  {
  481.   r = a;
  482.   for (b=0;b<(countw-1);b++)
  483.   {
  484.    point1=r; 
  485.    point2=r+counth;
  486.    fprintf(fp,"  EDGE Edge[%d] %d %d\n",edgecount++,point1,point2);
  487.    r=r+counth;
  488.   }  
  489.  }
  490.                               /* write vertical edges */
  491.  for (a=0; a<countw;a++)
  492.  {
  493.   r = a*counth;
  494.   for(b=0; b<(counth-1);b++)
  495.   {
  496.    point1=r+b; 
  497.    point2=r+b+1;
  498.    fprintf(fp,"  EDGE Edge[%d] %d %d\n",edgecount++,point1,point2);
  499.   }
  500.  }
  501.                                 /* write diagonal edges */
  502.  for (a=0; a<counth-1;a++)
  503.  {
  504.   r = a;
  505.   for (b=0;b<(countw-1);b++)
  506.   {
  507.    point1=r; 
  508.    point2=r+counth+1;
  509.    fprintf(fp,"  EDGE Edge[%d] %d %d\n",edgecount++,point1,point2);
  510.    r=r+counth;
  511.   }  
  512.  }
  513.      /* ohhh boy, THIS (^^^) took me time ! :), now to that faces! */
  514.  
  515.                                          /* write faces */
  516.  
  517.  faces = 2*(countw-1)*(counth-1); 
  518.  
  519.  fprintf(fp,"  FACE TCount %d\n",faces);
  520.  
  521.  r = 0;
  522.  r3 = countw*(counth-1)+counth*(countw-1);
  523.  
  524.  for(a=0; a<counth-1; a++)
  525.  {
  526.   r2=counth*(countw-1)+a;
  527.   for(b=0; b<countw-1; b++)
  528.   {
  529.    edge1 = r+countw-1;
  530.    edge2 = r2;
  531.    edge3 = r3;
  532.    fprintf(fp,"  FACE Connect[%d] %d %d %d\n",facecount++,edge1,edge2,edge3);
  533.  
  534.    edge1 = r;
  535.    edge2 = r2+counth-1;
  536.    edge3 = r3;
  537.    fprintf(fp,"  FACE Connect[%d] %d %d %d\n",facecount++,edge1,edge2,edge3);
  538.  
  539.    r++;
  540.    r2=r2+counth-1;
  541.    r3++;
  542.   }
  543.  } 
  544.  
  545.  fprintf(fp,"  COLR R=98 G=68 B=58\n");
  546.  fprintf(fp,"  REFL R=0 G=0 B=0\n");
  547.  fprintf(fp,"  TRAN R=0 G=0 B=0\n");
  548.  fprintf(fp,"  SPC1 R=0 G=0 B=0\n");
  549.  fprintf(fp,"  End DESC   \"Object 1 at level 1 of hierarchy 1\"\n");
  550.  fprintf(fp," TOBJ       \"Object 0 at level 1 of hierarchy 1\"\n");
  551.  fprintf(fp,"End OBJ  \"Hierarchy 1\"\n");
  552.  
  553.  
  554.  
  555. } /* writet3dfinish */
  556.  
  557.  
  558. /**********************************************/
  559. /* CalcCameraposition:                        */
  560. /*                                            */
  561. /**********************************************/
  562.  
  563. void CalcCameraposition(struct ShellyArguments *ShellyArgs)
  564.  
  565. {
  566.  double x,y,z,R,S,O;
  567.  double my, Re, P, W1, W2, N, L, g, k;
  568.  double smin, smax, sd, a, b, A, beta, phi, omega, alpha, omin, omax, od;   
  569.  double max = 0.0;
  570.  
  571.  smin = (*ShellyArgs).smin*pi/180;
  572.  smax = (*ShellyArgs).smax*pi/180;
  573.  omin = (*ShellyArgs).omin*pi/180;
  574.  omax = (*ShellyArgs).omax*pi/180;
  575.  sd = (*ShellyArgs).sd*pi/180;
  576.  od = (*ShellyArgs).od*pi/180;
  577.  
  578.  alpha = (*ShellyArgs).alpha*pi/180;
  579.  beta = (*ShellyArgs).beta*pi/180;
  580.  phi = (*ShellyArgs).phi*pi/180;
  581.  omega = (*ShellyArgs).omega*pi/180;
  582.  my = (*ShellyArgs).my*pi/180;
  583.  
  584.  a = (*ShellyArgs).a;
  585.  b = (*ShellyArgs).b;
  586.  A = (*ShellyArgs).A;
  587.  
  588.  N = (*ShellyArgs).N;
  589.  W1 = (*ShellyArgs).W1*pi/180;
  590.  W2 = (*ShellyArgs).W2*pi/180;
  591.  P = (*ShellyArgs).P*pi/180;
  592.  L = (*ShellyArgs).L;
  593.  
  594.  
  595.   fprintf(stderr,"Calculating Cameraposition ...");
  596.   fflush(stderr);
  597.  
  598.   for(O = omin; (O+od)<omax; O=O+od)
  599.   {
  600.    for(S = smin; (S+sd)<smax; S=S+sd)
  601.    {
  602.  
  603.     if (N == 0) 
  604.      g = 0;
  605.     else
  606.      g  = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi)));
  607.  
  608.     Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5); 
  609.     if (N == 0)
  610.      k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
  611.     else
  612.      k  = L*exp(-1*(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-1*(2*g/W2)*(2*g/W2));
  613.  
  614.  
  615.     R = Re + k;
  616.     
  617.     x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));   
  618.     y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
  619.     z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
  620.  
  621.     countp++;
  622.  
  623.     if (fabs(y)>fabs(z))
  624.     {
  625.      if (fabs(x)>fabs(y))
  626.      {
  627.       if(max<fabs(x)) 
  628.        max = fabs(x);
  629.      }  
  630.      else
  631.      {
  632.       if(max<fabs(y))
  633.        max = fabs(y);
  634.      }
  635.     }  
  636.     else
  637.      if (fabs(x)>fabs(z))
  638.      {
  639.       if(max<fabs(x)) 
  640.        max = fabs(x);
  641.      }  
  642.      else
  643.      {
  644.       if(max<fabs(z))
  645.        max = fabs(z);
  646.      }
  647.       
  648.    } /* for */
  649.   } /* for */
  650.  
  651.  
  652.   camy = 1.5*max;
  653.   camz = camy;
  654.  
  655.   fprintf(stderr,"\n");      /* rrrrready :) */
  656.  
  657. } /* CalcCameraposition */
  658.  
  659.  
  660. /**********************************************/
  661. /* RenderShell:                               */
  662. /*             SSIA :)                        */
  663. /**********************************************/
  664.  
  665. void RenderShell(struct ShellyArguments *ShellyArgs,char *fout)
  666.  
  667. {
  668.  struct punkt *List1anker, *List2anker, *L1p1, *L2p1, *L1p2, *L2p2;
  669.  
  670.  double x,y,z,R,S,O;
  671.  double my, Re, P, W1, W2, N, L, g, k;
  672.  double smin, smax, sd, a, b, A, beta, phi, omega, alpha, omin, omax, od;   
  673.  FILE *fp;
  674.  
  675.  smin = (*ShellyArgs).smin*pi/180;
  676.  smax = (*ShellyArgs).smax*pi/180;
  677.  omin = (*ShellyArgs).omin*pi/180;
  678.  omax = (*ShellyArgs).omax*pi/180;
  679.  sd = (*ShellyArgs).sd*pi/180;
  680.  od = (*ShellyArgs).od*pi/180;
  681.  
  682.  alpha = (*ShellyArgs).alpha*pi/180;
  683.  beta = (*ShellyArgs).beta*pi/180;
  684.  phi = (*ShellyArgs).phi*pi/180;
  685.  omega = (*ShellyArgs).omega*pi/180;
  686.  my = (*ShellyArgs).my*pi/180;
  687.  
  688.  a = (*ShellyArgs).a;
  689.  b = (*ShellyArgs).b;
  690.  A = (*ShellyArgs).A;
  691.  
  692.  N = (*ShellyArgs).N;
  693.  W1 = (*ShellyArgs).W1*pi/180;
  694.  W2 = (*ShellyArgs).W2*pi/180;
  695.  P = (*ShellyArgs).P*pi/180;
  696.  L = (*ShellyArgs).L;
  697.  
  698.  
  699.  if ((*ShellyArgs).output != RPL)
  700.   CalcCameraposition(ShellyArgs);
  701.  
  702.  fp = fopen(fout,"w");
  703.  if (fp != NULL) 
  704.  {
  705.   if ((*ShellyArgs).output == POV)
  706.    writepovheader(fp);
  707.   if ((*ShellyArgs).output == T3D)
  708.    writet3dheader(fp);
  709.    
  710.   fprintf(stderr,"Calculating the Shell ...\n");
  711.  
  712.   for(S = smin;(S+sd)<smax;S=S+sd)
  713.   {
  714.    if (N == 0) 
  715.     g = 0;
  716.    else
  717.     g  = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi))); 
  718.  
  719.    Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5); 
  720.  
  721.    if (N == 0)
  722.     k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
  723.    else
  724.     k  = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-(2*g/W2)*(2*g/W2));
  725.  
  726.    R = Re + k;
  727.  
  728.   /* alternatively: */
  729.   /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
  730.  
  731.    x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));   
  732.    y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
  733.    z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
  734.  
  735.    if ((L1p1 = calloc(1,sizeof(struct punkt))) == NULL)
  736.    {
  737.     fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
  738.     fclose(fp);
  739.     myfree(List2anker);
  740.     myfree(List1anker);
  741.     exit(5);
  742.    } /* if */
  743.  
  744.    if (S == smin) 
  745.     List1anker = L1p1;
  746.    else
  747.     (*L1p2).next = L1p1;
  748.    L1p2 = L1p1;
  749.  
  750.    (*L1p1).x = x;
  751.    (*L1p1).y = y;
  752.    (*L1p1).z = z;
  753.  
  754.   } /* for */
  755.  
  756.   if ((*ShellyArgs).output == RPL)
  757.    writerplline(fp,List1anker);
  758.   if ((*ShellyArgs).output == T3D)
  759.    writet3dline(fp,List1anker);
  760.  
  761.   for(O = omin+od;(O+od)<omax;O=O+od)
  762.   {
  763.  
  764.    counth = 0;
  765.  
  766.    for(S = smin;(S+sd)<smax;S=S+sd)
  767.    {
  768.  
  769.     counth++;                           /* calc number of points per line */
  770.   
  771.     if (N == 0) 
  772.      g = 0;
  773.     else
  774.      g  = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi)));
  775.  
  776.     Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5); 
  777.     if (N == 0)
  778.      k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
  779.     else
  780.      k  = L*exp(-1*(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-1*(2*g/W2)*(2*g/W2));
  781.  
  782.  
  783.     R = Re + k;
  784.  
  785.     /* alternatively: */
  786.     /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
  787.  
  788.     x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));   
  789.     y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
  790.     z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
  791.  
  792.     if ((L2p1 = calloc(1,sizeof(struct punkt))) == NULL)
  793.     {
  794.      fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
  795.      fclose(fp);
  796.      myfree(List1anker);
  797.      myfree(List1anker);
  798.      exit(5);
  799.     } /* if */
  800.  
  801.     if (S == smin)
  802.      List2anker = L2p1;
  803.     else
  804.      (*L2p2).next = L2p1;
  805.     L2p2 = L2p1;
  806.  
  807.     (*L2p1).x = x;
  808.     (*L2p1).y = y;
  809.     (*L2p1).z = z;
  810.  
  811.  
  812.    } /* for */
  813.  
  814.    if ((*ShellyArgs).output == POV)
  815.     writepovtriangles(fp,List1anker,List2anker);    
  816.    if ((*ShellyArgs).output == T3D)
  817.     writet3dline(fp,List2anker);
  818.    if ((*ShellyArgs).output == RPL)
  819.     writerplline(fp,List2anker);
  820.  
  821.    myfree(List1anker);
  822.    List1anker = List2anker;
  823.  
  824.  
  825.   } /* for */
  826.  
  827.   if ((*ShellyArgs).output == RPL)
  828.    writerplfinish(fp);
  829.   if ((*ShellyArgs).output == T3D)
  830.    writet3dfinish(fp);
  831.    
  832.   
  833.  
  834.   fprintf(stderr,"Ready!\n");
  835.  
  836.   myfree(List1anker);
  837.  
  838.   if (fclose(fp) != 0) 
  839.   fprintf(stderr,"Error while closing file: %s!\n",fout);
  840.  }
  841.  else
  842. fprintf(stderr,"Could not open outfile: %s!\n",fout);
  843.  
  844. } /* RenderShell */
  845.  
  846.  
  847.  
  848.  
  849. /********************************************/
  850. /*main:                                     */
  851. /********************************************/
  852. int main(int ac,char **av)
  853.  
  854. {
  855.  
  856.  struct ShellyArguments ShellyArgs;
  857.  
  858.  fprintf(stderr,"ShellyV1.2, the ShellShapeGenerator by RANDi\n");
  859.  
  860.  if (ac < 3)
  861.  {
  862.   fprintf(stderr,"wrong arguments!\nUSAGE:\n\n");
  863.   fprintf(stderr,"   'shelly infilename outfilename'\n\n");
  864.   exit(0);
  865.  } /* if */
  866.  
  867.  ReadInfile(&ShellyArgs,av[1]); 
  868.  
  869.  RenderShell(&ShellyArgs,av[2]); 
  870.  
  871.  exit(0);
  872. }
  873.